package edu.jamestang.yeb.controller;

import edu.jamestang.yeb.entity.Admin;
import edu.jamestang.yeb.entity.Role;
import edu.jamestang.yeb.service.AdminService;
import edu.jamestang.yeb.service.RoleService;
import edu.jamestang.yeb.util.Resp;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author JamesTang
 * @date 2021/12/16 20:01
 */

@RestController
@RequestMapping("/system/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "Get all operator")
    @GetMapping("/")
    public List<Admin> getAllAdmins(String keyword){
        return adminService.getAllAdmins(keyword);
    }

    @ApiOperation(value = "Update Admin")
    @PutMapping("/")
    public Resp updateAdmin(@RequestBody Admin admin){

        if(adminService.updateById(admin)){
            return Resp.success("Update successful");
        }
        return Resp.error("Update fail");
    }

    @ApiOperation(value = "Delete Admin")
    @DeleteMapping("/{id}")
    public Resp deleteAdmin(@PathVariable Integer id){

        if(adminService.removeById(id)){
            return Resp.success("Delete successful");
        }
        return Resp.error("Delete fail");
    }

    @ApiOperation(value = "Get all role")
    @GetMapping("/roles")
    public List<Role> getAllRoles(){
        return roleService.list();
    }

    @ApiOperation(value = "Update operator role")
    @PutMapping("/role")
    public Resp updateAdminRole(Integer adminId, Integer[] rids){
        return adminService.updateAdminRole(adminId, rids);
    }
}
